import type { ProColumns } from '@/components/Pro/';
import { ProTable } from '@/components/Pro/';
import { batteryFaultControllerGetCommunicateLossRate as getList } from '@/services/ess/diagnosisBatteryfault';
import type { DatePickerProps } from 'antd';
import moment from 'moment';
import React, { useState } from 'react';
import { useIntl } from 'umi';
import './Common.less';

const TableList: React.FC<{
  history?: boolean;
}> = ({ history }) => {
  const [currentMonth, setCurrentMonth] = useState('1');
  const intl = useIntl();
  const disabledDate: DatePickerProps['disabledDate'] = (current, { from }) => {
    const disabled = current && current > moment().subtract(1, 'days').endOf('day');

    if (from) {
      return disabled || Math.abs(current.diff(from, 'years')) >= 1;
    }

    return disabled;
  };

  const columns: ProColumns<API.CommunicateLossRateResponse>[] = [
    {
      title: intl.formatMessage({ id: 'global.s/N' }),
      dataIndex: 'pageIndex',
      hideInSearch: true,
    },
    {
      title: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.statistics' }),
      dataIndex: 'currentMonth',
      valueType: 'select',
      initialValue: '1',
      valueEnum: {
        1: {
          text: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.currentMonth' }),
        },
        2: {
          text: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.custom' }),
        },
      },
      hideInTable: true,
      fieldProps: {
        onChange: (v) => {
          setCurrentMonth(v);
        },
      },
    },
    {
      title: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.customTime' }),
      dataIndex: 'modifyTime',
      valueType: 'dateRange',
      initialValue: [moment().subtract(31, 'days'), moment().subtract(1, 'days')],
      fieldProps: {
        disabledDate: disabledDate,
        allowClear: false,
      },
      search: {
        transform: (value) => {
          return {
            ssDateStart: value[0],
            ssDateEnd: value[1],
          };
        },
      },
      hideInTable: true,
      hideInSearch: currentMonth === '1' ? true : false,
    },
    {
      title: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.occurrenceTime' }),
      dataIndex: 'ssDate',
      hideInSearch: true,
    },
    {
      title: intl.formatMessage({ id: 'diagnosticPrediction.diagnosis.packetLossRate' }),
      dataIndex: 'lossRate',
      hideInSearch: true,
      sorter: true,
      render: (_, record) =>
        record.lossRate !== undefined && record.lossRate !== null
          ? record.lossRate + '%'
          : undefined,
    },
  ];
  return (
    <div>
      <ProTable<API.CommunicateLossRateResponse, API.PageParams>
        headerTitle=" "
        rowKey="id"
        request={(params: API.batteryFaultControllerGetCommunicateLossRateParams, sort) => {
          if (params?.currentMonth === '1') {
            params.ssDateStart = moment().startOf('month').format('YYYY-MM-DD');
            params.ssDateEnd = moment().format('YYYY-MM-DD');
          }
          if (sort.lossRate === 'ascend') {
            params.lossRateSort = false;
          }
          if (sort.lossRate === 'descend') {
            params.lossRateSort = true;
          }
          setCurrentMonth(params?.currentMonth);
          delete params.currentMonth;
          return getList(params);
        }}
        params={
          history
            ? {
                isSolve: 3,
              }
            : {}
        }
        columns={columns}
        search={{
          className: 'searchPosInline',
        }}
        toolbar={{
          className: 'toobalrZindex',
          actions: [
            // eslint-disable-next-line react/button-has-type
          ],
        }}
      />
    </div>
  );
};
export default TableList;
